package com.ruoyi.module.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.ShoppingCartMapper;
import com.ruoyi.module.domain.ShoppingCart;
import com.ruoyi.module.service.IShoppingCartService;

/**
 * 我的购物车Service业务层处理
 *
 * @author fubojin
 * @date 2024-12-16
 */
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 查询我的购物车
     *
     * @param id 我的购物车主键
     * @return 我的购物车
     */
    @Override
    public ShoppingCart selectShoppingCartById(Long id) {
        return shoppingCartMapper.selectShoppingCartById(id);
    }

    /**
     * 查询我的购物车列表
     *
     * @param shoppingCart 我的购物车
     * @return 我的购物车
     */
    @Override
    public List<ShoppingCart> selectShoppingCartList(ShoppingCart shoppingCart) {
        return shoppingCartMapper.selectShoppingCartList(shoppingCart);
    }

    /**
     * 新增我的购物车
     *
     * @param shoppingCart 我的购物车
     * @return 结果
     */
    @Override
    public int insertShoppingCart(ShoppingCart shoppingCart) {
        ShoppingCart shoppingCartNew = new ShoppingCart();
        shoppingCartNew.setGoodsId(shoppingCart.getGoodsId());
        shoppingCartNew.setUserId(shoppingCart.getUserId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectShoppingCartList(shoppingCartNew);
        if (shoppingCarts == null || shoppingCarts.size() == 0) {
            shoppingCart.setCreateTime(DateUtils.getNowDate());
            return shoppingCartMapper.insertShoppingCart(shoppingCart);
        } else {
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            shoppingCart1.setQuantity(shoppingCart1.getQuantity()+shoppingCart.getQuantity());
            return shoppingCartMapper.updateShoppingCart(shoppingCart1);
        }
    }

    /**
     * 修改我的购物车
     *
     * @param shoppingCart 我的购物车
     * @return 结果
     */
    @Override
    public int updateShoppingCart(ShoppingCart shoppingCart) {
        return shoppingCartMapper.updateShoppingCart(shoppingCart);
    }

    /**
     * 批量删除我的购物车
     *
     * @param ids 需要删除的我的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShoppingCartByIds(Long[] ids) {
        return shoppingCartMapper.deleteShoppingCartByIds(ids);
    }

    /**
     * 删除我的购物车信息
     *
     * @param id 我的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShoppingCartById(Long id) {
        return shoppingCartMapper.deleteShoppingCartById(id);
    }
}
